﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GG.Base;
using System.Diagnostics;
using Pfz.RemoteGaming;
namespace GGSim
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
        internal static MainWindow instance;
        internal static RemoteGameClient game;

        public MainWindow()
        {
            instance = this;
            this.InitializeComponent();
            //game = new RemoteGameClient();

            this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);

            //game.Changed += new EventHandler<RemoteGameChangedEventArgs>(game_Changed);
            //game.Start("localhost", 578);

            Car car = new Car();
            isoGrid.AddGridDrawable(car);
            car.Rotation = 90;


        }

        void game_Changed(object sender, RemoteGameChangedEventArgs e)
        {
            if (e.AddedComponents != null || e.RemovedComponents != null)
            {
                Dispatcher.Invoke
                (
                    new Action
                    (
                        () =>
                        {
                            if (e.RemovedComponents != null)
                            {
                                foreach (var component in e.RemovedComponents)
                                {
                                    var drawable = component as IGridDrawable;
                                    if (drawable != null)
                                        isoGrid.RemoveGridDrawable(drawable);
                                }
                            }

                            if (e.AddedComponents != null)
                            {
                                foreach (var component in e.AddedComponents)
                                {
                                    var drawable = component as IGridDrawable;
                                    if (drawable != null)
                                    {
                                        isoGrid.AddGridDrawable(drawable);
                                    }
                                }
                            }
                        }
                    )
                );
            }

            Dispatcher.BeginInvoke
            (
                new Action
                (
                    () =>
                    {
                        isoGrid.InvalidateVisual();
                    }
                )
            );

        }

        void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left: isoGrid.TurnLeft(); break;
                case Key.Right: isoGrid.TurnRight(); break;
                case Key.Space: isoGrid.SwitchTopDown(isoGrid.LookOrientation + 45); break;
            }
        }
	}
}